package com.fractalwrench.harmonybootstrap.sample;

import com.beardedhen.harmonybootstrap.BootstrapDropDown;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BootstrapDropDownExample extends AbilitySlice {

    private List<BootstrapDropDown> bootstrapDropDownList = new ArrayList<BootstrapDropDown>();

    private static String[] getStringArray(Context context,
        int id) {//Pass openharmony context and Id as ResourceTable.Strarray_month_names
        ResourceManager manager = context.getResourceManager();
        try {
            return manager.getElement(id).getStringArray();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer rootLayout = (ComponentContainer) LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_example_bootstrap_dropdown, null, false);

        bootstrapDropDownList.add((BootstrapDropDown) rootLayout.findComponentById(ResourceTable.Id_xsmall));
        bootstrapDropDownList.add((BootstrapDropDown) rootLayout.findComponentById(ResourceTable.Id_small));
        bootstrapDropDownList.add((BootstrapDropDown) rootLayout.findComponentById(ResourceTable.Id_medium));
        bootstrapDropDownList.add((BootstrapDropDown) rootLayout.findComponentById(ResourceTable.Id_large));
        bootstrapDropDownList.add((BootstrapDropDown) rootLayout.findComponentById(ResourceTable.Id_xlarge));
        bootstrapDropDownList.add((BootstrapDropDown) rootLayout.findComponentById(ResourceTable.Id_left));
        bootstrapDropDownList.add((BootstrapDropDown) rootLayout.findComponentById(ResourceTable.Id_center));
        bootstrapDropDownList.add((BootstrapDropDown) rootLayout.findComponentById(ResourceTable.Id_right));
        bootstrapDropDownList.add((BootstrapDropDown) rootLayout.findComponentById(ResourceTable.Id_xsmall_1));
        bootstrapDropDownList.add((BootstrapDropDown) rootLayout.findComponentById(ResourceTable.Id_small_1));
        bootstrapDropDownList.add((BootstrapDropDown) rootLayout.findComponentById(ResourceTable.Id_medium_1));
        bootstrapDropDownList.add((BootstrapDropDown) rootLayout.findComponentById(ResourceTable.Id_large_1));
        bootstrapDropDownList.add((BootstrapDropDown) rootLayout.findComponentById(ResourceTable.Id_xlarge_1));
        setDropDownData();
        super.setUIContent(rootLayout);
    }

    private void setDropDownData() {
        for (BootstrapDropDown bootstrapDropDown : bootstrapDropDownList) {
            bootstrapDropDown.setDropdownData(
                getStringArray(getContext(), ResourceTable.Strarray_bootstrap_dropdown_example_data));
        }
    }
}
